package ch7.ex2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Vector;

import ch3.Sex;
import ch3.Student;

public class ClasTest2 {

	public static void main(String[] args) throws Exception, IllegalAccessException {
		Class c = Vector.class;
		// 调用无参的构造方法
		Vector v = (Vector) c.newInstance();
		v.add("222");
		System.out.println(v);

		Class c3 = Student.class;
		// >>>
		System.out.println("构造方法：");
		Constructor[] cr = c3.getDeclaredConstructors();
		for (Constructor c1 : cr)
			System.out.println(c1);
		System.out.println("属性：");
		Field[] fr = c3.getDeclaredFields();
		for (Field f : fr)
			System.out.println(f);
		System.out.println("方法：");
		Method[] mr = c3.getDeclaredMethods();
		for (Method m : mr)
			System.out.println(m);
		// Student st = (Student) c3.newInstance();
		// System.out.println(st);

		System.out.println(">>>>>>>>>>>>>>>>>>");
		Constructor s3 = c3.getDeclaredConstructor(String.class, Sex.class, int.class);
		s3.setAccessible(true);// 去掉访问限制
		Student st = (Student) s3.newInstance("王明", Sex.男, 27);
		System.out.println(st);
		System.out.println(">>>>>>>>>>>>.");
		Field fname = c3.getDeclaredField("name");
		fname.setAccessible(true);
		fname.set(st, "张顺");
		System.out.println(st);
		System.out.println(">???????????????????/");
		Method mage = c3.getDeclaredMethod("setAge", int.class);
		mage.invoke(st, 23);// st.setAge(23)
		System.out.println(st);
	}

}
